﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmKho : Form
    {
        private KhoService httService = new KhoService();

        public void LoadThongTinKho(DataTable dbtab)
        {
            lv_Kho.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                lv_Kho.Items.Add(liv);
            }
        }
        public frmKho()
        {
            InitializeComponent();
        }

        private void frmKho_Load(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LayKho();
            LoadThongTinKho(dbtab);
            LoadNhaHang();
        }

        private void LoadNhaHang()
        {
            NhaHangService NCC = new NhaHangService();
            cmbNH.DisplayMember = "TenNH";
            cmbNH.ValueMember = "MaNH";
            //cmbNH.Items.Add("Tất cả");
            cmbNH.DataSource = NCC.LoadNH();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            String tenKho = txt_TenKho.Text.ToString();
            //MessageBox.Show(cmbLoaiNL.SelectedValue.ToString());
            if (tenKho != "")
            {
                int sucChua = int.Parse(txt_SucChua.Text.ToString());
                int maNH = int.Parse(cmbNH.SelectedValue.ToString());
                if (!httService.ThemKho(tenKho, sucChua, maNH))
                {
                    MessageBox.Show("Thêm Không Thành Công");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Tên Kho Rỗng");
            }
            DataTable dbtab = httService.LayKho();
            LoadThongTinKho(dbtab);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (lv_Kho.FocusedItem != null)
            {
                string ten = txt_TenKho.Text;
                int sucChua = int.Parse(txt_SucChua.Text);
                int maNH = int.Parse(cmbNH.SelectedValue.ToString());
                if (ten != "")
                {
                    int index = lv_Kho.FocusedItem.Index;
                    int ma = int.Parse(lv_Kho.Items[index].Text.ToString());
                    if (!httService.SuaKho(ma, ten, sucChua, maNH))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        return;
                    }
                    DataTable dbtab = httService.LayKho();
                    LoadThongTinKho(dbtab);
                }
                else
                {
                    MessageBox.Show("Tên kho trống");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn kho");
                return;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lv_Kho.FocusedItem != null)
            {
                int index = lv_Kho.FocusedItem.Index;
                int ma = int.Parse(lv_Kho.Items[index].Text.ToString());
                if (!httService.XoaKho(ma))
                {
                    MessageBox.Show("Xóa Không Thành Công");
                    return;
                }
                DataTable dbtab = httService.LayKho();
                LoadThongTinKho(dbtab);
            }
            else
            {
                MessageBox.Show("Chưa chọn Kho");
                return;
            }
        }

        private void lv_Kho_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lv_Kho.FocusedItem != null)
                {
                    int index = lv_Kho.FocusedItem.Index;
                    int ma = int.Parse(lv_Kho.Items[index].Text.ToString());
                    DataTable dbtab = httService.LayKhoTheoMa(ma);
                    DataRow dbrow = dbtab.Rows[0];
                    txt_TenKho.Text = dbrow[1].ToString();
                    txt_SucChua.Text = dbrow[2].ToString();
                    cmbNH.SelectedValue = dbrow[3].ToString();
                }
            }
            catch (Exception ex)
            {
                return;
                throw ex;
            }
        }

        private void btnChiTietKho_Click(object sender, EventArgs e)
        {

        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LayKho();
            LoadThongTinKho(dbtab);
            //LoadNhaHang();
        }

        private void cmbNH_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
